/**
 * ajax_comment
 * 
 * @version 2.0.2
 * @author KM@INN STUDIO
 * @changelog
 *	2.0.2	2012.10.18
 * 			fix some words error
 * 
 */
define(function(require, exports, module){
	
	/**
	 * Init
	 */
	exports.init = function(){
		jQuery(document).ready(function(){
			exports.comments_pagenavi.init();
			exports.comments_add.init();
		});
	};
	/**
	 * Config 
	 */
	exports.config = {
		add_comment_link_id : '.add_comment_link',
		respond_id : '#respond',
		commentform_id : '#commentform',
		commentlist_id : '.commentlist',
		comment_count_num_id : '.comment_count_num .comment_cnt',
		comment_id : '#comment',
		comment_post_ID_id : '#comment_post_ID',
		comment_parent_id : '#comment_parent',
		submit_id : '#commentform :submit',
		
		comment_wrap_id : '.comment_wrap',
		comments_pagenavi_id : '.comments_pagenavi',
		comments_pagenavi_a_id : '.comments_pagenavi a',
		/**
		 * The language config
		 */
		lang : {
			M00001 : 'Commented successfully, thanks your participation!'
		}
		
	
	};
	
	/**
	 * Comments pagenavi
	 */
	exports.comments_pagenavi = {
		/**
		 * Init
		 */
		init : function(){
			exports.comments_pagenavi.bind();
		
		},
		/**
		 * Bind elements
		 */
		bind : function(){
			jQuery(exports.config.comments_pagenavi_a_id).off().on('click',function(){
				var $this = jQuery(this),
					url = $this.attr('href'),
					ajax_data = {
						action : 'get_comments_list'
					},
					passed = true,
					dialog;
				/** do a filter */
				passed = exports.comments_pagenavi.hook.check_passed(passed,$this,dialog);
				if(!passed) return false;
				/**
				 * Start AJAX
				 */
				require.async(['art','art_css','status_tip','lang'],function(_a,_b,status_tip,SYSMSG){
					_a(jQuery);
					jQuery.ajax({
						url:url,
						type:'post',
						dataType:'html',
						data:ajax_data,
						beforeSend:function(){
							dialog = art.dialog({
								id:'comments_pagenavi',
								title: false,
								content:status_tip('loading',SYSMSG.M00001),
								fixed:true,
								drag:false
							});
						},success:function(html){
							exports.comments_pagenavi.hook.send_success(html,$this,dialog);
						},error:function(e,xhr,opt){
							exports.comments_pagenavi.hook.send_error(e,xhr,opt,$this,dialog);
						}
					});
				});
				return false;
			});
		},
		
		
		hook : {
			check_passed : function(passed,$this,dialog){
				if(!exports.comments_pagenavi.hook.move_form(passed,$this,dialog)) return false;
				jQuery(exports.config.commentlist_id).fadeTo('slow','0.5');

				return passed;
			},
			/**
			 * Move the respond form
			 */
			move_form : function(passed,$this,dialog){
				/** on reply status, must to move the from */
				if(jQuery('#wp-temp-form-div')[0]){
					jQuery('#wp-temp-form-div').after(jQuery(exports.config.respond_id)).remove();
					jQuery('#cancel-comment-reply-link').hide();
				}
				return passed;
			},
			send_success : function(html,$this,dialog){
				/** InnerHTML */
				jQuery(exports.config.comment_wrap_id).html(html);
				jQuery(exports.config.commentlist_id).fadeTo('fast','1');
				
				
				/* 滚动 */
				jQuery('html,body').animate({
					scrollTop : jQuery(exports.config.comment_wrap_id).offset().top - 30
				},300);
				/** Rebind the pagenavi */
				exports.init()
				
				require.async(['art','art_css'],function(_a,_b){
					_a(jQuery);
					/** Close the dialog */
					dialog.close();
				});
			},
			
			send_error : function(e,xhr,opt,$this,dialog){
				require.async(['art','art_css','status_tip','lang'],function(_a,_b,status_tip,SYSMSG){
					_a(jQuery);
					dialog.content(status_tip('error',SYSMSG.E00001));
					dialog.time(3000);
				});
				jQuery(exports.config.commentlist_id).fadeTo('fast','1');
			}
		
		}
	};
	
	/**
	 * Comments ADD
	 */
	exports.comments_add = {
		/**
		 * Init
		 */
		init : function(){
			exports.comments_add.bind.add_link();
			exports.comments_add.bind.frm_submit();
		},
		/**
		 * Bind elements
		 */
		bind : {
			/**
			 * Bind the click when the add link was clicked
			 */
			add_link : function(){
				jQuery(exports.config.add_comment_link_id).off().on('click',function(){
					jQuery('html,body').animate({
						scrollTop : jQuery(exports.config.respond_id).offset().top - 50
					},300);
					/** Set the form focus */
					require.async(['check'],function(check){
						check.auto_focus(jQuery(exports.config.commentform_id));
					});
					return false;
				});
			},
			/**
			 * Bind the comment submit
			 */
			frm_submit : function(){
			// alert('a');
				jQuery(exports.config.commentform_id).off().on('submit',function(){
								// alert('a');

					var $this = jQuery(this),
						passed = true,
						dialog;
					/** Use async to check and submit */
					passed = exports.comments_add.hook.check_passed(passed,$this,dialog);
					if(!passed) return false;
					
					/**
					 * Start Ajax submit
					 */
					require.async(['art','art_css','lang','status_tip'],function(_a,_b,SYSMSG,status_tip){
						_a(jQuery);
						var ajax_data = $this.serialize();
						jQuery.ajax({
							url:SYSMSG.G00001,
							type:'post',
							dataType:'json',
							data:ajax_data,
							beforeSend:function(){
								dialog = art.dialog({
									id:'comments_ajax',
									title: false,
									content:status_tip('loading',SYSMSG.M00002),
									fixed: true,
									drag: false
								});
								jQuery(':submit',$this).attr('disabled','true').fadeTo('slow','0.5');
							},success:function(data){
								/* add a hook for 'comments_ajax_success' */
								exports.comments_add.hook.send_success(data,$this,dialog);
							},error:function(e,xhr,opt){
								/* add a hook for 'comments_ajax_error' */
								exports.comments_add.hook.send_error(e,xhr,opt,$this,dialog);
							}
						});/** End Ajax */
					});/** End require.async */
					return false;
				});
			}
		
		},
		
		hook : {
			check_passed : function(passed,$this,dialog){
				if(!exports.comments_add.hook.blank_value(passed,$this,dialog))return false;
				return passed;
			},
			/** Check the blank value */
			blank_value : function(passed,$this,dialog){
				var check = require('check'),
					frm_obj = check.frm_is_valid($this);
				return frm_obj.is_invalid ? false : passed;
			},
			/** SUCCESS */
			send_success : function(data,$this,dialog){
				require.async(['art','art_css','status_tip','lang'],function(_a,_b,status_tip,SYSMSG){
					_a(jQuery);
					var default_content,
						register_callback;
					jQuery(':submit',$this).removeAttr('disabled').fadeTo('slow','1');
					/** The callback data is SUCCESS */
					if(data.status === 'success'){
						jQuery(exports.config.comment_id).val('');
						var $new_comment = jQuery(data.des.content);
						$new_comment.hide();
						$new_comment.fadeIn();
						/**
						 * is Reply comment status
						 */
						if(data.des.comment_parent){
							var $parent_comment = jQuery(exports.config.respond_id).parent();
							/**
							 * Define new class
							 */
							var new_class = 'even';
							if($parent_comment.hasClass('even')){
								new_class = 'odd';
							}
							var new_depth_class = $parent_comment.attr('class').match(/depth-\d+/g).join('');
							new_depth_class = parseInt(new_depth_class.match(/\d+/g).join('')) + 1;
							new_depth_class = 'depth-' + new_depth_class;
							
							var $new_comment_li = $new_comment.find('.comment');
							$new_comment_li.removeClass('depth-1').addClass(new_class + ' ' + new_depth_class);
							$new_comment.html($new_comment_li);
							/** Inner the reply html */
							jQuery(exports.config.respond_id).before($new_comment);
							/** Reset the Respond form position */
							var $respond = jQuery(exports.config.respond_id);
							$respond.find('#comment_parent').val('0');
							$respond.find('#cancel-comment-reply-link').hide();
							$respond.insertBefore(jQuery('#wp-temp-form-div'));
							jQuery('#wp-temp-form-div').remove;
							
						/**
						 * is Add Comment status
						 */
						}else{
							var $last_comment = jQuery(exports.config.commentlist_id).find('.comment:last-child');
							if($last_comment.hasClass('even') && $last_comment.hasClass('thread-even')){
								jQuery($new_comment).addClass('odd thread-odd');
							}else{
								jQuery($new_comment).addClass('even thread-even');
								
							}
							/** Show the new comment */
							jQuery(exports.config.commentlist_id).show().append($new_comment);
						}
						/**
						 * Scroll eff
						 * For ie678, they are not support offset().top
						 */
						try{
							jQuery('html,body').animate({
								scrollTop : $new_comment.offset().top - 30
							},300);
						}catch(e){}
						/** Update the Count number */
						var new_count = parseInt(jQuery(exports.config.comment_count_num_id).text()) + 1;
						jQuery(exports.config.comment_count_num_id).text(new_count);
						
						/** Define the success content */
						default_content = status_tip('success',exports.config.lang.M00001);
					
					/**
					 * the callback data is error
					 */
					}else{
						if(data.des && data.des.content){
							default_content = status_tip('error',data.des.content);
						}else{
							default_content = status_tip('error',SYSMSG.E00001);
						}
					}
					dialog.content(default_content);
					dialog.time(2000);
				});
			},
			/** The Send Error */
			send_error : function(e,xhr,opt,$this,dialog){
				require.async(['art','art_css','status_tip','lang'],function(_a,_b,status_tip,SYSMSG){
					_a(jQuery);
					jQuery(':submit',$this).removeAttr('disabled').fadeTo('slow','1');
					dialog.content(status_tip('error',SYSMSG.E00001));
					dialog.time(3000);
				});
			}
		}
	
	};
	
	/**
	 * move the comment form
	 */
	addComment = {
		moveForm : function(commId, parentId, respondId, postId) {
			var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');

			if ( ! comm || ! respond || ! cancel || ! parent )
				return;

			t.respondId = respondId;
			postId = postId || false;

			if ( ! t.I('wp-temp-form-div') ) {
				div = document.createElement('div');
				div.id = 'wp-temp-form-div';
				div.style.display = 'none';
				respond.parentNode.insertBefore(div, respond);
			}

			comm.parentNode.insertBefore(respond, comm.nextSibling);
			if ( post && postId )
				post.value = postId;
			parent.value = parentId;
			cancel.style.display = '';

			cancel.onclick = function(){
				t.cancel_reply(this);
				return false;
			}
			try {
				require.async(['check'],function(check){
					check.auto_focus(jQuery(exports.config.commentform_id));
				});
			}catch(e) {}

			return false;
		},

		I : function(e) {
			return document.getElementById(e);
		},
		
		cancel_reply : function($this){
			var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId);

			if ( ! temp || ! respond )
				return;

			t.I('comment_parent').value = '0';
			temp.parentNode.insertBefore(respond, temp);
			temp.parentNode.removeChild(temp);
			$this.style.display = 'none';
			$this.onclick = null;
			return false;
		}
		
	}
});